//
//  HMEmoticonKeyBoard.swift
//  Weibo
//
//  Created by fuyu on 2017/8/4.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit
/*
 1. 表情视图-> UICollectionView
 2. 页数指示器-> UIPageControl
 3. toolbar->UIStackView
 */

//  自定义表情键盘
class HMEmoticonKeyBoard: UIView {
    
    //  MARK: --   懒加载
    fileprivate lazy var toolBar: HMEmoticonToolBar = HMEmoticonToolBar()
    
    //  表情视图
    fileprivate lazy var emoticonView: UICollectionView = {
        let rect = CGRect(origin: CGPoint.zero, size: CGSize(width: self.width, height: self.height - 35))
        let flowLayout = UICollectionViewFlowLayout()
        let collectionView: UICollectionView = UICollectionView(frame: rect, collectionViewLayout: flowLayout)
        collectionView.backgroundColor = self.backgroundColor
        //  设置itemsize的大小
        flowLayout.itemSize = collectionView.size
        //  设置最小行间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //  水平方向滚动
        flowLayout.scrollDirection = .horizontal
        
        //  隐藏滚动条
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        //  开启分页
        collectionView.isPagingEnabled = true
        //  取消弹簧效果
        collectionView.bounces = false
        //  注册cell
        collectionView.register(HMEmoticonViewCell.self, forCellWithReuseIdentifier: EmoticonViewCellIdentifier)
        //  设置数据源代理
        collectionView.dataSource = self
        //  设置代理
        collectionView.delegate = self
        return collectionView
    }()
    
    //  页数指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let pageCtr = UIPageControl()
        //  当前指示颜色
        //        pageCtr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //        //  其它指示颜色
        //        pageCtr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        //  kvc给oc成员变量赋值
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        //  隐藏单页
        pageCtr.hidesForSinglePage = true
        return pageCtr
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  记录成员变量个数
        var count: UInt32 = 0
        //  获取类里面成员变量，提示： swift没有成员变量，只有属性，所以swift类型不能获取成员变量
        let ivarList = class_copyIvarList(UIPageControl.self, &count)!
        //  变量成员变量
        for i in 0..<count {
            //  根据成员变量的下标获取对员变量
            let ivar = ivarList[Int(i)]
            //  获取成员变量对应名字
            let ivarName = ivar_getName(ivar)!
            //  转成对应String的字符串
            let ivarNameStr = String(cString: ivarName)
            //  获取成员变量的类型
            let ivarType = ivar_getTypeEncoding(ivar)
            //  转成对应String的字符串
            let ivarTypeStr = String(cString: ivarType!)
            print(ivarNameStr, ivarTypeStr)
            //            _currentPageImage @"UIImage"
            //            _pageImage @"UIImage"
            
        }

        let normalIndexPath = IndexPath(item: 0, section: 1)
        //  默认显示默认这组表情
        emoticonView.scrollToItem(at: normalIndexPath, at: [], animated: false)
        //  绑定页数指示器数据
        setPageControlData(indexPath: normalIndexPath)
        //  设置背景色
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonView)
        addSubview(pageControl)
        addSubview(toolBar)
        
        pageControl.snp.makeConstraints { (make) in
            make.bottom.equalTo(emoticonView)
            make.centerX.equalTo(emoticonView)
            make.height.equalTo(10)
        }
        
        toolBar.snp.makeConstraints { (make) in
            make.bottom.leading.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        //  设置闭包
        toolBar.clickToolBarButtonClosure = {[unowned self] (type: HMEmoticonToolBarButtonType) in
            let indexPath: IndexPath
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            //  滚动到指定的indexPath
            self.emoticonView.scrollToItem(at: indexPath, at: [], animated: false)
        }
    }
    //  根据indexPath绑定数据
    fileprivate func setPageControlData(indexPath: IndexPath) {
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }

}

//  MARK: --UICollectionViewDataSource
extension HMEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmoticonViewCellIdentifier, for: indexPath) as! HMEmoticonViewCell
        
        //  获取cell所需要表情数组
        let emotionArray = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        cell.indexPath = indexPath
        //  绑定数据
        cell.emoticonArray = emotionArray
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        //  获取水平方向内容中心点
        let contentCenterX = scrollView.width * 0.5 + scrollView.contentOffset.x
        //  获取垂直方向内容中心点
        let contentCenterY = scrollView.height * 0.5
        //  获取内容中心点
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
        //  根据内容中点获取对于的indexPath
        if let indexPath = emoticonView.indexPathForItem(at: contentCenter) {
            setPageControlData(indexPath: indexPath)
            toolBar.selectButtonWith(section: indexPath.section)
        }
        
        
    }
    
    
    
}
